using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using SpringWaterDataAccess;
using Utilities;

namespace sow
{
	/// <summary>
	/// Summary description for EditPartner.
	/// </summary>
	public partial class EditForumCategories : System.Web.UI.Page
	{
		protected SQLAccess sql;
		protected DataSet dsForum;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			
			GetSource();
				
			if (!IsPostBack)
			{
				DataGrid1.EditItemIndex = -1;
				BindData();
				
			}
			
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected int GetForumIDIndex(object ForumID)
		{
			if(dsForum == null)
				return -1;
			
			int index = 0;
			foreach( DataRow dr in dsForum.Tables[0].Rows)
			{
				if (dr["ForumID"].ToString() == ForumID.ToString())
					break;
				index++;
			}

			if (index == dsForum.Tables[0].Rows.Count)
				index = -1;
			return index;
		}

			
		private void GetSource()
		{
			sql = new SQLAccess();
			DataGrid1.DataSource = sql.getForumCategoriesData();
			DataGrid1.DataKeyField = "CategoryID";
		
		}
		private void BindData()
		{
			DataGrid1.DataBind();
		
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			
			

			//int CategoryID = (int)DataGrid1.DataKeys[dgSelectedIndex];
			//int ForumID = int.Parse(DropDownList1.Items[dlSelectedIndex].Value.ToString());
			//string CategoryName = DropDownList1.Items[dlSelectedIndex].Text;
			
			if (sql == null)
				sql = new SQLAccess();
			
			//sql.UpdateForumCategoryData(CategoryID, ForumID);
			//lblMessage.Text = dgSelectedIndex.ToString() + ":" + 
			//	dlSelectedIndex.ToString() + ":" + CategoryID.ToString() + ":" + ForumID.ToString();
			//lblMessage.Text += ":" + DataGrid1.DataKeys[dgSelectedIndex].ToString();
			GetSource();
			BindData();
			

		}

		

		
		private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGrid1.EditItemIndex = e.Item.ItemIndex;
			//int UserID = (int) DataGrid1.DataKeys[e.Item.ItemIndex];

			if (sql == null)
				sql = new SQLAccess();

			dsForum = sql.getForumData();
			//Append ForumID in the ForumName field so that can see both values
			foreach( DataRow dr in dsForum.Tables[0].Rows)
			{
				dr["ForumName"] += "(" + dr["ForumID"].ToString() + ")";
			}
		
			BindData();			
			lblMessage.Text = "Editing!";
		}

		private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGrid1.EditItemIndex = -1;
			BindData();
		}

		private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			int CategoryID = int.Parse(((Label)e.Item.FindControl("lblCategoryID")).Text); 
					
			int ForumID =int.Parse(((DropDownList)e.Item.FindControl("drpForumName")).SelectedValue); 
			
			if (sql == null)
				sql = new SQLAccess();
			
			sql.UpdateForumCategoryData(CategoryID, ForumID);
			
			DataGrid1.EditItemIndex = -1;

			GetSource();
			BindData();
		}

		
	}
}
